package org.ossean.gather.targetDao;

import java.util.Set;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.ossean.gather.model.GatherProject;
import org.ossean.gather.model.JobRequirement;
import org.ossean.gather.model.RelativeMemo;
import org.ossean.gather.model.Taggings;

public interface TargetDao {

	// 获得relative_memos中已存在的url_md5集合
	@Select("SELECT url_md5 AS md5 FROM ${targetTable}")
	public Set<String> initUrlMd5Set(@Param("targetTable") String targetTableName);

	// 将获取到得数据源数据插入目标表中
	@Insert("INSERT INTO ${targetTable} (${targetFields}) VALUES (#{model.id},#{model.title},#{model.content},#{model.created_time},#{model.updated_time},"
			+ "#{model.memo_type},#{model.tags},#{model.source},#{model.url},#{model.url_md5},#{model.author},#{model.author_url},#{model.view_num},#{model.review_num},#{model.extracted_time})")
	public int insertRelativeMemo(@Param("targetTable") String targetTableName,
			@Param("targetFields") String targetFields,
			@Param("model") RelativeMemo model);

	// 对urlMD5相同的数据进行update操作
	@Update("update ${targetTable} set id=#{model.id},title=#{model.title},content=#{model.content},created_time=#{model.created_time},updated_time=#{model.updated_time},"
			+ "memo_type=#{model.memo_type},tags=#{model.tags},source=#{model.source},url=#{model.url},url_md5=#{model.url_md5},author=#{model.author},author_url=#{model.author_url},"
			+ "view_num=#{model.view_num},review_num=#{model.review_num},extracted_time=#{model.extracted_time} where id=#{id}")
	public void updateRelativeMemo(
			@Param("targetTable") String targetTableName,
			@Param("model") RelativeMemo model, @Param("id") int id);
	
	// 对urlMD5相同的数据进行update操作
	@Update("update ${targetTable} set id=#{model.id},title=#{model.title},content=#{model.content},created_time=#{model.created_time},updated_time=#{model.updated_time},"
			+ "memo_type=#{model.memo_type},tags=#{model.tags},source=#{model.source},url=#{model.url},url_md5=#{model.url_md5},author=#{model.author},author_url=#{model.author_url},"
			+ "view_num=#{model.view_num},review_num=#{model.review_num},extracted_time=#{model.extracted_time} where url_md5=#{model.url_md5}")
	public Integer updateRelativeMemoByUrlMD5(
			@Param("targetTable") String targetTableName,
			@Param("model") RelativeMemo model, @Param("id") int id);
	@Update("update ${targettable} set id = #{id} where url_md5 = #{url_md5}")
	   public void updatePkControlTable(@Param("targettable")String targettable,@Param("id")int id,
			                            @Param("url_md5")String url_md5);
	
// 查找relative_memos表对应url_md5的记录
	@Select("select * from ${table} where url_md5=#{url_md5}")
	public RelativeMemo selectRelativeMemosItemByUrlMD5(
			@Param("table") String table, @Param("url_md5") String url_md5);

	// 向tag表存储数据
	@Insert("insert ignore into ${table} (name) values (#{name})")
	public void insertTag(@Param("table") String table,
			@Param("name") String name);

	// 根据tag的name找tag的id
	@Select("select id from ${table} where name=#{name}")
	public int selectTagIdByName(@Param("table") String table,
			@Param("name") String name);

	// 插入Taggings对象
	@Insert("insert ignore into ${table} (tag_id,taggable_id,taggable_type,context,created_at,disagree_num) values (#{model.tag_id},#{model.taggable_id},#{model.taggable_type},#{model.context},#{model.created_at},#{model.disagree_num})")
	public void insertTaggings(@Param("table") String table,
			@Param("model") Taggings model);

	// 删除gather_projects表中对应id的数据
	@Delete("delete from ${table} where id=#{id}")
	public void deleteOpenSourceProjectsItem(@Param("table") String table,
			@Param("id") int id);

	// 查找gather_projects表对应id的记录
	@Select("select * from ${table} where id=#{id}")
	public GatherProject selectOpenSourceProjectsItem(
			@Param("table") String table, @Param("id") int id);
	
	// 查找relative_memos表对应id的记录
	@Select("select * from ${table} where id=#{id}")
	public RelativeMemo selectRelativeMemosItem(
			@Param("table") String table, @Param("id") int id);

	/**
	 * 下面是transferProjects程序的函数
	 */

	// 将获取到得数据源数据插入目标表中
	@Insert("INSERT INTO ${targetTable} (${targetFields}) VALUES (#{model.id},#{model.name},#{model.tags},#{model.url},#{model.url_md5},"
			+ "#{model.description},#{model.language},#{model.source},#{model.license},#{model.homepage},#{model.updated_time},#{model.extracted_time},#{model.created_time})")
	public int insertOpenSourceProject(
			@Param("targetTable") String targetTableName,
			@Param("targetFields") String targetFields,
			@Param("model") GatherProject model);

	// 对urlMD5相同的数据进行update操作
	@Update("update ${targetTable} set id=#{model.id},name=#{model.name},tags=#{model.tags},url=#{model.url},url_md5=#{model.url_md5},"
			+ "description=#{model.description},language=#{model.language},source=#{model.source},license=#{model.license},homepage=#{model.homepage},updated_time=#{model.updated_time},extracted_time=#{model.extracted_time},"
			+ "created_time=#{model.created_time},update_mark=#{model.update_mark} where id=#{id}")
	public void updateOpenSourceProject(
			@Param("targetTable") String targetTableName,
			@Param("model") GatherProject model, @Param("id") int id);

	// 将获取到得数据源数据插入目标表中
	@Insert("INSERT INTO ${targetTable} (${targetFields}) VALUES (#{model.id},#{model.title},#{model.content},#{model.created_time},#{model.type},"
			+ "#{model.tags},#{model.url},#{model.url_md5},#{model.author},#{model.author_url},#{model.view_num},#{model.review_num},#{model.extracted_time}"
			+ ",#{model.source},#{model.similar_position},#{model.work_place},#{model.experience},#{model.salary},#{model.scale},#{model.domain}"
			+ ",#{model.finance},#{model.style},#{model.education})")
	public int insertJobRequirement(
			@Param("targetTable") String targetTableName,
			@Param("targetFields") String targetFields,
			@Param("model") JobRequirement model);

	// 对urlMD5相同的数据进行update操作
	@Update("update ${targetTable} set id=#{model.id},title=#{model.title},content=#{model.content},created_time=#{model.created_time},type=#{model.type},"
			+ "tags=#{model.tags},url=#{model.url},url_md5=#{model.url_md5},author=#{model.author},author_url=#{model.author_url},view_num=#{model.view_num},"
			+ "review_num=#{model.review_num},extracted_time=#{model.extracted_time},source=#{model.source},similar_position=#{model.similar_position}"
			+ ",work_place=#{model.work_place},experience=#{model.experience},salary=#{model.salary},scale=#{model.scale},domain=#{model.domain}"
			+ ",finance=#{model.finance},style=#{model.style},education=#{model.education} where id=#{id}")
	public void updateJobRequirement(
			@Param("targetTable") String targetTableName,
			@Param("model") JobRequirement model, @Param("id") int id);

	// 查找刚刚插入gather_projects表中的记录id
	@Select("select id from ${table} where name=#{model.name} order by id desc limit 1")
	public int getAutoIncrementOspId(@Param("table") String table,
			@Param("model") GatherProject model);

	// 根据urlMD5码找到RelativeMemo对象
	@Select("select * from ${table} where url_md5=#{urlMD5}")
	public RelativeMemo findPostByUrlMD5(@Param("table") String table,
			@Param("urlMD5") String urlMD5);

	// 根据urlMD5码找到OpenSourceProject对象
	@Select("select * from ${table} where url_md5=#{urlMD5}")
	public GatherProject findPrjByUrlMD5(@Param("table") String table,
			@Param("urlMD5") String urlMD5);

	// 根据urlMD5码找到JobRequirement对象
	@Select("select * from ${table} where url_md5=#{urlMD5}")
	public JobRequirement findJobByUrlMD5(@Param("table") String table,
			@Param("urlMD5") String urlMD5);
    @Delete("delete from ${gatherPostsTableName} where id = #{model.id} and url_md5 != #{model.url_md5}")
	public void deleteWhileExist(@Param("gatherPostsTableName")String gatherPostsTableName, @Param("model")RelativeMemo model);

}
